﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using log4net;
using System.Configuration;
using VehicleConfigurator.Database;
using VehicleConfigurator.Model;
using System.Threading;

namespace VehicleConfigurator
{
    public partial class frmLogin : Form
    {
        static readonly ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); //Constructing log
        public frmLogin()
        {
            InitializeComponent();
            this.maskedTextBox1.PromptChar = ' ';
            this.AcceptButton = this.btnOk;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            
            try
            {
               // MessageBox.Show(this.maskedTextBox1.Text);
                //this.txtPassword.Text = "abc13579"; // <-- Sao sep lai fix the nay ^_^
                clsVariable.Server = txtServer.Text;
                clsVariable.DBName = cmbDb.Text;
                clsVariable.DBPass = txtPassword.Text;
                clsVariable.DBUserName = txtUser.Text;
                clsVariable.Language = cmbLang.Text;
                clsVariable.Port = maskedTextBox1.Text.Trim();
                clsDb db = new clsDb();
                if (db.DBConn())
                {
                    if (!this.cmbDb.Items.Contains(cmbDb.Text))
                    {
                        this.cmbDb.Items.Add(cmbDb.Text);
                    }
                    FrmMain frmMain = new FrmMain();
                    frmMain.Show();
                    this.Hide();
                }
                else
                {
                    this.cmbDb.Items.Remove(cmbDb.Text);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }



        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            try
            {
                //Thread.CurrentThread.CurrentCulture.ClearCachedData();
                var dbList = Properties.Settings.Default["dbList"] as string;

                this.txtServer.Text = Properties.Settings.Default["serverName"] as string;
                this.txtUser.Text = Properties.Settings.Default["userName"] as string;
                this.cmbLang.Text = Properties.Settings.Default["userLanguage"] as string;
                this.cmbDb.Text = Properties.Settings.Default["userDb"] as string;
                this.maskedTextBox1.Text = Properties.Settings.Default["port"] as string;

                if (!string.IsNullOrEmpty(dbList))
                {
                    this.cmbDb.Items.Clear();
                    var items = dbList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    if (items.Length > 0)
                    {
                        this.cmbDb.Items.AddRange(items);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Info(ex.Message, ex);
            }
        }

        private void frmLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                var dbLen = this.cmbDb.Items.Count;
                if (dbLen > 0)
                {
                    var dbList = new string[dbLen];
                    this.cmbDb.Items.CopyTo(dbList, 0);
                    Properties.Settings.Default["dbList"] = string.Join(",", dbList);
                    Properties.Settings.Default["serverName"] = txtServer.Text;
                    Properties.Settings.Default["userName"] = txtUser.Text;
                    Properties.Settings.Default["userLanguage"] = cmbLang.Text;
                    Properties.Settings.Default["userDb"] = cmbDb.Text;
                    Properties.Settings.Default["port"] = maskedTextBox1.Text;
                    // save user settings
                    Properties.Settings.Default.Save();
                }

            }
            catch (Exception ex)
            {
                logger.Info(ex.Message, ex);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
